<template>
  <div class="box" >
    <div ref="vantaRef" style="width: 100%; height: 97vh"></div>
    <div class="banner">
      <h1>购物系统</h1>
      <h6>
        &ensp;&ensp;GOU&ensp;&ensp;&ensp;WU
      </h6>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
          <el-input
              ref="userName"
              v-model="loginForm.userName"
              placeholder="Username"
              name="userName"
              type="text"
              tabindex="1"
              auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
          <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
        <el-button type="text" style="font-size: small;margin-bottom: 0px" @click="click2">注册</el-button>
      </el-form>

      <el-dialog title="请填写基本信息"  :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="customer">
          <el-form-item label="用户名" label-width="15%">
            <el-input v-model="customer.userName" autocomplete="off" style="width: 90%" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" label-width="15%">
            <el-input v-model="customer.password" autocomplete="off" style="width: 90%" placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" label-width="15%">
            <el-input v-model="checkPassword" autocomplete="off" style="width: 90%" placeholder="请再次输入密码" show-password></el-input>
          </el-form-item>
          <el-form-item label="姓名" label-width="15%">
            <el-input v-model="customer.cusName" autocomplete="off" style="width: 90%" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" label-width="15%">
            <el-input v-model="customer.cusPho" autocomplete="off" style="width: 90%" placeholder="请输入电话号码"></el-input>
          </el-form-item>
          <el-form-item label="住址" label-width="15%">
            <el-input v-model="customer.cusAdd" autocomplete="off" style="width: 90%" placeholder="请输入地址"></el-input>
          </el-form-item>
          <el-form-item label="邮编" label-width="15%">
            <el-input v-model="customer.cusPost" autocomplete="off" style="width: 90%" placeholder="请输入地址"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="click1">取 消</el-button>
          <el-button type="primary"  @click="save">注册</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import * as THREE from "three";
import BIRDS from "vanta/src/vanta.birds";
import { setToken } from '@/utils/auth'
import { validUsername } from '@/utils/validate'
import {login, save} from '@/api/customer'

export default {
  data() {
    name="login";

    //用户名校验规则
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }

    //用户名校验规则
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度至少为6位'))
      } else {
        callback()
      }
    }

    //数据模型
    return {
      customer:{
      },
      dialogFormVisible:false,
      loginForm: {
        userName: '123456',
        password: '123456'
      },
      loginRules: {
        userName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      checkPassword:''
    }
  },

  methods: {
    click1(){
      this.customer= {}
    },
    //注册
    click2(){
      this.dialogFormVisible=true;
    },
    save(){
          if(this.customer.password !== this.checkPassword){
            this.$message({
              message: "两次密码输入不一致",
              type: "error",
            })
            return;
          }
          save(this.customer).then((result) => {
            console.log(result)
            if (result.data.code == 1) {
              this.dialogFormVisible=false;
              this.$message({
                message:"注册成功",
                type:"success",
              })
            }
          }).catch(error => {
            if (error.response) {
              // 请求已发出，但服务器返回状态码不在 2xx 范围内
              console.log(error.response.data);
              console.log(error.response.status);
              console.log(error.response.headers);
            } else if (error.request) {
              // 请求已发出，但没有收到响应
              console.log(error.request);
              // 显示自定义的 404 页面
              window.location.href = '/404';
            } else {
              // 发生了其他错误
              console.log('Error', error.message);
            }
          });
    },
    //展示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {

        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

/*npm install vanta@0.5.24
npm install three@0.121.0*/
    //登录方法
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true

          //调用登录后端接口
          login(this.loginForm).then((result) => {
            console.log(result)
            if (result.data.code == 1) {
              setToken(result.data.data);
              console.log('login success');
              //console.log("result",result)
              this.$router.push('/');
            } else {
              this.$message.error(result.data.msg);
              this.loading = false
            }
          }).catch(error => {
            if (error.response) {
              // 请求已发出，但服务器返回状态码不在 2xx 范围内
              console.log(error.response.data);
              console.log(error.response.status);
              console.log(error.response.headers);
            } else if (error.request) {
              // 请求已发出，但没有收到响应
              console.log(error.request);
              // 显示自定义的 404 页面
              window.location.href = '/404';
            } else {
              // 发生了其他错误
              console.log('Error', error.message);
            }
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  mounted() {
    this.vantaEffect = BIRDS({
      el: this.$refs.vantaRef,
      THREE: THREE,
    });
    // 修改颜色时 cells 需要全大写字母 可生效
    VANTA.BIRDS({
      el: this.$refs.vantaRef,
      mouseControls: true,
      touchControls: true,
      gyroControls: false,
      minHeight: 200.00,
      minWidth: 200.00,
      scale: 1.00,
      scaleMobile: 1.00,
      backgroundColor: 0x154ea0,
      color1: 0xc52929
    });
  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy();
    }
  },
};
</script>

<style lang="less" scoped>
.box {
  position: relative;


  .banner {
    z-index: 999;
    position: absolute;
    top: 30%;
    left: 10%;
    color: #fff;
  }
}

h1 {
  font-size: 66px;
}

p {
  margin-top: 60px;
  font-size: 18px;
}
</style>

